Last active
April 22, 2017 05:47
-
-
Save masaponto/c41334c49204cb8882a19afae9ee5d0c to your computer and use it in GitHub Desktop.
MESHボタン押すとラズパイが天気しゃべるよ (http://masaponto.hatenablog.com/entry/2017/04/21/234752)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bottledaemon import daemon_run | |
from bottle import route, run, get | |
from talk_weather import talk_weather, talk_datetime | |
@get('/test') | |
def test(): | |
return 'Hello Raspbery Pi!' | |
@get('/talk_weather') | |
def talk_today_weather(): | |
talk_datetime() | |
talk_weather() | |
if __name__ == "__main__": | |
daemon_run(host='0.0.0.0', port=8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Reference: http://raspi.seesaa.net/article/415482141.html | |
# 引数チェック | |
CMDNAME=`basename $0` | |
if [ $# -lt 1 ]; then | |
echo "Usage: ${CMDNAME} [ text ]" 1>&2 | |
exit 1 | |
fi | |
# 定数定義(出力ファイル名、辞書の場所、音声データの場所) | |
TMPFILE=`mktemp /tmp/tmp.XXXXXXXX.wav` | |
DIC=/var/lib/mecab/dic/open-jtalk/naist-jdic | |
VOICE=/usr/share/hts-voice/mei/mei_normal.htsvoice | |
# 音声データ生成 | |
echo "$1" | open_jtalk \ | |
-x ${DIC} \ | |
-m ${VOICE} \ | |
-ow ${TMPFILE} && \ | |
# 生成した音声データを再生する | |
aplay --quiet ${TMPFILE} | |
# 生成した音声データを削除する | |
rm -f ${TMPFILE} | |
# 終了 | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var apiURL = "http://<your-host>/talk_weather"; | |
ajax ({ | |
url : apiURL, | |
type : "get", | |
timeout : 10000, | |
success : function ( contents ) { | |
callbackSuccess( { | |
resultType : "continue", | |
} ); | |
}, | |
error : function ( request, errorMessage ) { | |
log("Day : Network error"); | |
callbackSuccess( { | |
resultType : "continue", | |
} ); | |
} | |
}); | |
return { | |
resultType : "continue" | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return { | |
resultType : "continue" | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
""" | |
Reference: http://raspi.seesaa.net/article/415530289.html | |
""" | |
import shlex | |
import subprocess | |
from datetime import datetime | |
import urllib2 | |
import json | |
CMD_TALK = 'jtalk' | |
def talk_datetime(): | |
d = datetime.now() | |
text = '%s月%s日、%s時%s分' % (d.month, d.day, d.hour, d.minute) | |
text = CMD_TALK + ' ' + text | |
print text | |
proc = subprocess.Popen(shlex.split(text)) | |
proc.communicate() | |
def talk_weather(): | |
city = '070030' # Aizu | |
json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1' | |
weather_text = u'%sの天気は%sです。' | |
temperature_text = u'予想最高気温は、%s度、予想最低気温は、%s度です。' | |
try: | |
r = urllib2.urlopen('%s?city=%s' % (json_url, city)) | |
obj = json.loads(unicode(r.read())) | |
forecasts = obj['forecasts'] | |
cast = forecasts[0] | |
temperature = cast['temperature'] | |
today_w_txt = weather_text % (cast['dateLabel'], cast['telop']) | |
if temperature['max'] is not None and temperature['min'] is not None: | |
today_t_txt = temperature_text % ( | |
temperature['max']['celsius'], temperature['min']['celsius']) | |
weather_str = today_w_txt + ' ' + today_t_txt | |
else: | |
weather_str = today_w_txt | |
weather_str = weather_str.encode('utf-8') | |
text = CMD_TALK + ' ' + weather_str | |
print text | |
proc = subprocess.Popen(shlex.split(text)) | |
proc.communicate() | |
finally: | |
r.close() | |
def main(): | |
talk_datetime() | |
talk_weather() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment